<?php

/**
 * This is the model class for table "zy_distribution_gift_number".
 *
 * The followings are the available columns in table 'zy_distribution_gift_number':
 * @property integer $gift_id
 * @property string $partners_id
 * @property integer $number
 * @property string $provide_time
 * @property integer $using_state
 * @property string $backup1
 * @property string $backup2
 * @property integer $is_delete
 * @property string $crate_time
 * @property string $crate_user
 * @property string $update_time
 * @property string $update_user
 */
class DistributionGiftNumber extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return DistributionGiftNumber the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_distribution_gift_number';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('number, using_state, is_delete', 'numerical', 'integerOnly'=>true),
			array('partners_id, crate_user, update_user', 'length', 'max'=>50),
			array('backup1, backup2', 'length', 'max'=>500),
			array('provide_time, crate_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('gift_id, partners_id, number, provide_time, using_state, backup1, backup2, is_delete, crate_time, crate_user, update_time, update_user', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'gift_id' => Yii::t('base','Gift Id'),
			'partners_id' => Yii::t('base','Partners'),
			'number' => Yii::t('base','Number'),
			'provide_time' => Yii::t('base','Provide Time'),
			'using_state' => Yii::t('base','Using State'),
			'backup1' => Yii::t('base','Describe'),
			'backup2' => 'Backup2',
			'is_delete' => 'Is Delete',
			'crate_time' => 'Crate Time',
			'crate_user' => 'Crate User',
			'update_time' => 'Update Time',
			'update_user' => 'Update User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('gift_id',$this->gift_id);
		if(yii::app()->user->level() == 9){
			$criteria->compare('partners_id',yii::app()->user->id,true);
		}else{
			$criteria->compare('partners_id',$this->partners_id,true);
		}
		$criteria->compare('number',$this->number);
		$criteria->compare('provide_time',$this->provide_time,true);
		$criteria->compare('using_state',0);
		$criteria->compare('backup1',$this->backup1,true);
		$criteria->compare('backup2',$this->backup2,true);
		$criteria->compare('is_delete',0);
		$criteria->compare('crate_time',$this->crate_time,true);
		$criteria->compare('crate_user',$this->crate_user,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user',$this->update_user,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 获取合作商
	 */
	function getPartnersName($user_id){
		if(!empty($user_id)){
			$sql="select * from zy_user where user_id = '".$user_id."'";
			$rs=Yii::app()->db->createCommand($sql)->queryAll();
			return $rs[0]['user_name'];
		}else{
			$sql="select * from zy_user where level = 9";
			$rs=Yii::app()->db->createCommand($sql)->queryAll();
			return CHtml::listData($rs,'user_id','user_name');
		}
	}
	
	/**
	 * 获取礼品描述
	 */
	function getGiftDescribe($id){
		$sql="select * from zy_distribution_gift_number where gift_id='".$id."'";
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
		return $rs[0]['backup1'];
	}
	/**
	 * 使用状态
	 */
	function getUseingStateCnName($num){
		if($num != ""){
			if($num == 0){
				return Yii::t('base','There Balances');
			}else{
				return Yii::t('base','No Balances');
			}
		}
		
	}
}